//
//  DropMenu.swift
//  soapp
//
//  Created by zhu xietong on 2017/10/13.
//

import UIKit
import Eelay


open class DropMenu: UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    public var menu_width:CGFloat = UIScreen.main.bounds.width
    public var menu_height:CGFloat = 44
    public var control:DropContentControl
    
    public init() {
        control = DropContentControl(with: nil)
        super.init(frame: .zero)
        control.menu = self
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public var items = [DropItem]()
    {
        didSet{
            self.loadItems(list: items)
        }
    }
    
    public var padding:UIEdgeInsets = .zero
    
    public var space:CGFloat = 1
    
    public var config:(UICollectionView)->Void = {_ in}
    
    lazy var collectionView:UICollectionView = { [unowned self] in
        let views = self.subviews.filter {$0 is UICollectionView } as! [UICollectionView]
        if views.count != 0 {
            return views.first!
        }
        
        let flow = UICollectionViewFlowLayout()
        flow.scrollDirection = .horizontal
        flow.minimumInteritemSpacing = 1
        flow.minimumLineSpacing = 1
        flow.estimatedItemSize = CGSize(width: Swidth/4, height: 40)
        let p = self.padding

        let v = UICollectionView(frame: [p.top,0,Swidth,40], collectionViewLayout: flow)
        v.register(DropItemCell.self, forCellWithReuseIdentifier: "cell")
        v.delegate = self
        v.dataSource = self
        self.config(v)
        self.eelay = [
            [v,[ee.T.L.B.R,[p.top,p.left,-p.bottom,-p.right]]]
        ]
        
        return v
    }()
    
    func itemSize(index:Int) -> CGSize {
        var flex_width:CGFloat = self.menu_width - (padding.left + padding.right) - space * items.count.cg_floatValue
        var abs_width_count = 0
        items.forEach { (item) in
            if let _ = item.width
            {
                abs_width_count += 1
            }
            flex_width = flex_width - (item.width ?? 0.0)
        }
        let uasb_width = flex_width/(items.count - abs_width_count).cg_floatValue
        return CGSize(width: items[index].width ?? uasb_width, height: (menu_height - self.padding.top - self.padding.bottom))
    }
    

    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return itemSize(index: indexPath.item)
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .zero
    }
    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! DropItemCell
        cell.item = items[indexPath.item]
        return cell
    }
    
    open func loadItems(list:[DropItem]){
        
        collectionView.reloadData()
    }

    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
       
        let item = items[indexPath.item]
        if !item.isSelected
        {
            control.open(item: item)
        }
        else{
            control.close()
        }
        for one in items
        {
            if one !== item
            {
                one.isSelected = false
            }
        }
    }
    
    open override var intrinsicContentSize: CGSize{
        return CGSize(width: menu_width, height: menu_height)
    }
}
